package cup.com.icss.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cup.com.icss.dao.MailSystemMapper;
import cup.com.icss.dao.UserMapper;
import cup.com.icss.model.MailSystem;
import cup.com.icss.model.User;
import cup.com.icss.utils.LogUtil;
import cup.com.icss.utils.StringUtil;

@Controller
public class mailSystemController {
	@Resource
	private UserMapper userMapper;
	@Resource
	private MailSystemMapper mailSystemMapper;
	
	@RequestMapping("/mailSystemAdd")
	@ResponseBody
	public ModelAndView mailSystemAdd(HttpServletRequest request,String studentId){
		ModelAndView view = new ModelAndView();
		try {
			User Fuser = (User)request.getSession().getAttribute("USER");
			view.addObject("FstudentId",Fuser.getStudentId());
			view.addObject("FcnName",Fuser.getCnName());
			view.addObject("Fcollege",Fuser.getCollege());
			view.addObject("FtheClass",Fuser.getTheClass());
			
			User Juser = userMapper.selectByPrimaryKey(studentId);
			view.addObject("JstudentId",Juser.getStudentId());
			view.addObject("JcnName",Juser.getCnName());
			view.addObject("Jcollege",Juser.getCollege());
			view.addObject("JtheClass",Juser.getTheClass());
			
			view.setViewName("cup/jsp/mailSystem/mailSystemAdd");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/mailSystemAddDate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> mailSystemAddDate(HttpServletRequest request,MailSystem record){
		Map<String, String> map = new HashMap<String, String>();
		try {
			Date time = new Date();
			record.setId(StringUtil.getUUID());
			record.setTime(time);
			record.setStatus("1");
			int inster = mailSystemMapper.insertSelective(record);
			if(inster>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/mailSystemList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> mailSystemList(HttpServletRequest request,Integer page,Integer rows,MailSystem record){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			if(rows!=null && page!=null){
				page=page==null?1:page;
				rows=rows==null?10:rows;
				int pageNum = page;
				int pageSize = rows;
				int num = mailSystemMapper.selectMailSystemListSizedatagrid(record).size();
				int pageSizez;
				if(((pageSize*pageNum)-num)<0){
					pageSizez = pageSize;
				}else{
					pageSizez = num-(10*(pageNum-1));
				}
				List<MailSystem> theAnnouncement = mailSystemMapper.selectMailSystemListdatagrid(pageNum*pageSize-pageSize,pageSizez,record);
				map.put("total", num);
				map.put("rows", theAnnouncement);
			}else{
				record.setStatus("1");
				int num = mailSystemMapper.selectMailSystemListSize(record).size();
				List<MailSystem> theAnnouncement = mailSystemMapper.selectMailSystemList(0,6,record);
				map.put("rows", theAnnouncement);
				map.put("total", num);
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/mailSystemShow",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView mailSystemShow(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			MailSystem record = mailSystemMapper.selectByPrimaryKey(id);
			view.addObject("studentId",user.getStudentId());
			view.addObject("mailSystem",record);
			view.setViewName("cup/jsp/mailSystem/mailSystemShow");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/mailSystemShowdialog",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView mailSystemShowdialog(HttpServletRequest request,String id){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			MailSystem record = mailSystemMapper.selectByPrimaryKey(id);
			view.addObject("studentId",user.getStudentId());
			view.addObject("mailSystem",record);
			view.setViewName("cup/jsp/mailSystem/mailSystemShowdialog");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/mailSystemUpdata",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> mailSystemUpdata(HttpServletRequest request,MailSystem record){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int update = mailSystemMapper.updateByPrimaryKeySelective(record);
			if(update>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping("/mailSystemListDatagrid")
	@ResponseBody
	public ModelAndView mailSystemListDatagrid(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
		try {
			User user = (User)request.getSession().getAttribute("USER");
			view.addObject("jStudentId",user.getStudentId());
			view.setViewName("cup/jsp/mailSystem/mailSystemList");
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return view;
	}
	
	@RequestMapping(value="/mailSystemDelete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> mailSystemDelete(HttpServletRequest request,String id){
		Map<String, String> map = new HashMap<String,String>();
		try {
			int delete = mailSystemMapper.deleteByPrimaryKey(id);
			if(delete>0){
				map.put("status", "200");
			}else{
				map.put("status", "500");
			}
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
	
	@RequestMapping(value="/titleNum",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> titleNum(HttpServletRequest request,String jStudentId){
		Map<String, Object> map = new HashMap<String,Object>();
		try {
			int num;
			if(mailSystemMapper.selectTitleNum(jStudentId)==null){
				num=0;
			}else{
				num = mailSystemMapper.selectTitleNum(jStudentId).size();
			}
			map.put("num", num);
		} catch (Exception e) {
			// TODO: handle exception
			LogUtil.errorLog(e);
		}
		return map;
	}
}
